﻿using JYH.Framework;
using JYH.Model;
using JYH.Model.API;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.Xml.Linq;
using static JYH.Win.Main.Login;
using System.Net.Http;
using Newtonsoft.Json.Linq;
using JYH.Win.Public;

namespace JYH.Win.Main
{
    public partial class Login : FormBase
    {
        public Login()
        {
            InitializeComponent();
        }
  
        ApiClient apiClient = new ApiClient();


        private void Login_Load(object sender, EventArgs e)
        {
            apiClient=new ApiClient();
            Url = ConfigHelper.GetValue("WebAPIServer");

        }

        private async void btnLogin_Click(object sender, EventArgs e)
        {
            apiClient = new ApiClient();
            string UserNO = string.Empty;
            string Pwd = string.Empty;
            if (!string.IsNullOrEmpty(txtUserNO.Text))
            {
                UserNO = txtUserNO.Text.Trim();
            }
            if (!string.IsNullOrEmpty(txtPwd.Text))
            {
                Pwd = txtPwd.Text.Trim();
            }
            SysUserInfo data = new SysUserInfo
            {
                UserNO = UserNO,
                Pwd = Pwd
            };
          
            try
            {
               var response= await apiClient.PostAsync(Url + "/api/WIN/Login", data);
               object obj= JsonConvert.DeserializeObject(response);
             

                // 重新序列化为不带转义符的字符串

                ResponseJson<SysUserInfo> user = JsonConvert.DeserializeObject<ResponseJson<SysUserInfo>>(obj.ToString());
                if (user != null && user.Items!=null&&user.Items.Count>0) 
                {
                   this.CurrentUser= user.Items[0];
                    this.DialogResult=DialogResult.OK;
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"请求失败: {ex.Message}");
            }
        }


private void txtUserNO_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter) 
            {
                txtPwd.Focus();
            }
        }
    }
}
